/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年9月21日
 * @version 0.1
 */
package com.suredy.resource.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.core.service.BaseSrvWithEntity;
import com.suredy.resource.entity.SegmentEntity;
import com.suredy.resource.model.Segment;


/**
 * @author ZhangMaoren
 *
 */
@Service("SegmentSrv")
public class SegmentSrv extends BaseSrvWithEntity<SegmentEntity> {

	public DetachedCriteria getDc(SegmentEntity t) {
		DetachedCriteria dc = super.getDc(t);
		if (t == null) {
			return dc;
		}
		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		if (!StringUtils.isBlank(t.getUri())) {
			dc.add(Restrictions.eq("uri", t.getUri()));
		}

		return dc;
	}
	
	public List<Segment> getAll() {
		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("sort", false);
		List<SegmentEntity> entities = this.readByEntity(null, order);
		if (entities == null || entities.isEmpty()) {
			return null;
		}
		List<Segment> ret = new ArrayList<Segment>();
		for (SegmentEntity entity : entities) {
			ret.add(entity.toVO());
		}
		return ret;
	}

	public Segment getById(String segementId) {
		SegmentEntity entity = this.get(segementId);
		if (entity == null) {
			return null;
		}
		return entity.toVO();
	}
	
	public Segment getByUri(String uri) {
		SegmentEntity search = new SegmentEntity();
		search.setUri(uri);
		SegmentEntity entity = this.readSingleByEntity(search);
		if (entity == null) {
			return null;
		}
		return entity.toVO();
	}

}
